// Copyright 2008 Silvio Massari
//
// This file is part of SilvioMassari.Automata
//
// SilvioMassari.Automata is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SilvioMassari.Automata is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SilvioMassari.Automata.  If not, see <http://www.gnu.org/licenses/>.
				
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace SilvioMassari.Automata
{
	public class Pattern<S> : IEnumerable<Element<S>>
	{
		protected Element<S>[] _pattern = null;

		protected Pattern()
		{
		}

		public Pattern(Element<S>[] pattern)
		{
			if (pattern == null || pattern.Length == 0)
			{
				throw new ArgumentNullException("pattern");
			}

			_pattern = pattern;
		}

		public Element<S>[] Value
		{
			get { return _pattern; }
		}

		public int Length
		{
			get { return _pattern == null ? 0 : _pattern.Length; }
		}
			
		public override string ToString()
		{
			if (_pattern == null)
			{
				return null;
			}

			StringBuilder sb = new StringBuilder();
			foreach (Element<S> data in _pattern)
			{
				sb.Append(data.ToString());
			}
			return sb.ToString();
		}

		public Element<S> this [int index]
		{
			get 
			{ 
				if (_pattern==null || index<0 || index>=_pattern.Length)
				{
					throw new IndexOutOfRangeException();
				}
				return _pattern[index];
			}
			set 
			{
				if (_pattern==null || index<0 || index>=_pattern.Length)
				{
					throw new IndexOutOfRangeException();
				}
				_pattern[index] = value;
			}
		}

		#region IEnumerable<Element<S>> Members

		public IEnumerator<Element<S>> GetEnumerator()
		{
			if (_pattern == null)
			{
				yield break;
			}

			foreach (Element<S> element in _pattern)
			{
				yield return element;
			}
		}

		#endregion

		#region IEnumerable Members

		IEnumerator IEnumerable.GetEnumerator()
		{
			if (_pattern == null)
			{
				return null;
			}

			return ((IEnumerable)_pattern).GetEnumerator();
		}

		#endregion
	}


}
